package net.xujialiang.XTestRPA.Controller.RPAManager;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.RPA.XRPATagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/rpatag")
@RestController
public class RpaTagMgr {

    @Autowired
    XRPATagService tagSvc;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultData create(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String tagName = (String) reqBody.get("tagName");
        Integer mOrder = (Integer)reqBody.get("mOrder");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String createAccount = sUser.getUsername();
        String ttc = sUser.getTTC();
        try {
            tagSvc.createTag(tagName, createAccount, mOrder, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultData delete(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer id = (Integer) reqBody.get("id");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            tagSvc.deleteTag(id, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/fetch", method = RequestMethod.POST)
    @ResponseBody
    public ResultData<Map<String, Object>> fetch(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer page = (Integer) reqBody.get("page");
        Integer pageSize = (Integer) reqBody.get("pageSize");
        String tagName = (String) reqBody.get("tagName");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();

        try {
            Map<String, Object> ret = tagSvc.fetchTagsByPage(ttc, tagName, page, pageSize);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public ResultData order(@RequestBody Map<Integer, Integer> reqBody, HttpServletRequest request, Authentication auth) {
        try {
            SecurityUser sUser = (SecurityUser) auth.getPrincipal();
            String ttc = sUser.getTTC();
            tagSvc.updateTagOrder(reqBody, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultData update(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String newTagName = (String) reqBody.get("tagName");
        Integer id = (Integer) reqBody.get("id");
        Integer mOrder = (Integer) reqBody.get("mOrder");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            tagSvc.updateTag(id, newTagName, mOrder, ttc);
            return ResultData.success();
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }


}